<template>
  <div>
    <!-- <group> -->
    <x-address
      @on-hide="logHide"
      @on-show="logShow"
      :title="title"
      v-model="value"
      :list="addressData"
      @on-shadow-change="onShadowChange"
	  @input="changePicker"
      placeholder="请选择地址"
    ></x-address>
    <!-- </group> -->
  </div>
</template>
<script>
import { ChinaAddressV4Data, XAddress } from "vux";
export default {
  props: {
    backDataType: {
      //父组件调用的数据格式 name value name+value
      type: String,
      default: "",
    },
    province: {
      type: [String, Number],
      default: "",
    },
    city: {
      type: [String, Number],
      default: "",
    },
    county: {
      type: [String, Number],
      default: "",
    },
  },
  components: {
    XAddress,
  },
  data() {
    return {
      title: "",
      value: this.initValue(),
      addressData: ChinaAddressV4Data,
    };
  },
  methods: {
    initValue() {
      // console.log(this.province,this.city,this.county)
      let arr = [];
      if (this.province) {
        arr[0] = String(this.province);
        if (this.city) {
          arr[1] = String(this.city);
        }
        if (this.county) {
          arr[2] = String(this.county);
        }
      }
      return arr;
    },
    onShadowChange(ids, names) {},
    getName(value) {
      let backData = [];
      if (this.backDataType == "value") {
        backData = value;
      } else {
        let arr = [];
        for (let i in value) {
          for (let j in this.addressData) {
            if (value[i] === this.addressData[j].value) {
              arr.push(this.addressData[j]);
            }
          }
        }
        if (this.backDataType == "name") {
          backData = arr.map((item) => {
            return item.name;
          });
        } else {
          backData = arr;
        }
      }
      return backData;
    },
    logHide(str) {
    //   console.log(this.value);
      this.$emit("input", this.getName(this.value));
    },
    logShow(str) {},
	changePicker(val) {
		if (val[0]) {
        this.$emit("update:province", value[0]);
      }
      if (val[1]) {
        this.$emit("update:city", value[1]);
      }
      if (val[2]) {
        this.$emit("update:county", value[2]);
      }
		// console.log(val)
	}
  },
};
</script>

<style>
</style>
